/* -*-java-*-  Prototype file of KM-yacc parser for Java.
 *
 * Written by MORI Koichiro
 *
 * This file is PUBLIC DOMAIN.
 */


/* Here goes %{ ... %} */

package jp.seraph.jsade.sexpression;
// 必要なimportを書く
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class AbstractParser {


  public static final int YYERRTOK = 256;
  public static final int ATOM = 257;


  /*
    #define yyclearin (yychar = -1)
    #define yyerrok (yyerrflag = 0)
    #define YYRECOVERING (yyerrflag != 0)
    #define YYERROR  goto yyerrlab
  */


  /** Debug mode flag **/
  static boolean yydebug = false;

  /** lexical element object **/
  private Object yylval;

  /** Semantic value */
  private Object yyval;

  /** Semantic stack **/
  private Object yyastk[];

  /** Syntax stack **/
  private short yysstk[];

  /** Stack pointer **/
  private int yysp;

  /** Error handling state **/
  private int yyerrflag;

  /** lookahead token **/
  private int yychar;

  /* code after %% in *.y */

  protected void yyerror(String msg){}
  protected int yylex(){ return 0; }
  protected void setYYLVAL(Object aObject){
  	yylval = aObject;
  }

  protected Object getYYVAL(){
  	return yyval;
  }


  private static final byte yytranslate[] = {
      0,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      3,    4,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    5,    5,    5,    5,
      5,    5,    5,    5,    5,    5,    1,    2
  };
  private static final int YYBADCH = 5;
  private static final int YYMAXLEX = 258;
  private static final int YYTERMS = 5;
  private static final int YYNONTERMS = 7;

  private static final short yyaction[] = {
     14,    4,    0,   11
  };
  private static final int YYLAST = 4;

  private static final byte yycheck[] = {
      2,    3,    0,    4
  };

  private static final short yybase[] = {
     -2,   -1,   -2,    2,    0,    0,   -2
  };
  private static final int YY2TBLSTATE = 2;

  private static final short yydefault[] = {
  32767,32767,    1,32767,    7
  };

  private static final short yygoto[] = {
     13,    8
  };
  private static final int YYGLAST = 2;

  private static final byte yygcheck[] = {
      4,    4
  };

  private static final short yygbase[] = {
      0,    0,    0,    0,   -1,    0,    0
  };

  private static final short yygdefault[] = {
  -32768,    2,    1,    3,    7,    9,   10
  };

  private static final byte yylhs[] = {
      0,    3,    1,    1,    4,    4,    5,    2,    2,    6
  };

  private static final byte yylen[] = {
      1,    1,    1,    2,    1,    1,    3,    0,    2,    1
  };
  private static final int YYSTATES = 12;
  private static final int YYNLSTATES = 5;
  private static final int YYINTERRTOK = 1;
  private static final int YYUNEXPECTED = 32767;
  private static final int YYDEFAULT = -32766;

  private static final int YYDEFAULTSTACK = 512;

  /* Grow syntax and sematic stacks */
  private void growStack() {
    short[] tmpsstk = new short[yysp * 2];
    Object[] tmpastk = new Object[yysp * 2];
    for (int i = 0; i < yysp; i++) {
      tmpsstk[i] = yysstk[i];
      tmpastk[i] = yyastk[i];
    }
    yysstk = tmpsstk;
    yyastk = tmpastk;
  }

  /*
   * Parser entry point
   */
  public int yyparse() {
    int yyn;
    int yyp;
    int yyl;

    yyastk = new Object[YYDEFAULTSTACK];
    yysstk = new short[YYDEFAULTSTACK];

    int yystate = 0;
    int yychar1 = yychar = -1;

    yysp = 0;
    yysstk[yysp] = 0;
    yyerrflag = 0;
    for (;;) {
      if (yybase[yystate] == 0)
        yyn = yydefault[yystate];
      else {
        if (yychar < 0) {
          if ((yychar = yylex()) <= 0) yychar = 0;
          yychar1 = yychar < YYMAXLEX ? yytranslate[yychar] : YYBADCH;
        }

        if (((yyn = yybase[yystate] + yychar1) >= 0
             && yyn < YYLAST && yycheck[yyn] == yychar1
             || (yystate < YY2TBLSTATE
                 && (yyn = yybase[yystate + YYNLSTATES] + yychar1) >= 0
                 && yyn < YYLAST && yycheck[yyn] == yychar1))
            && (yyn = yyaction[yyn]) != YYDEFAULT) {
          /*
           * >= YYNLSTATE: shift and reduce
           * > 0: shift
           * = 0: accept
           * < 0: reduce
           * = -YYUNEXPECTED: error
           */
          if (yyn > 0) {
            /* shift */
            if (++yysp >= yysstk.length)
              growStack();

            yysstk[yysp] = (short)(yystate = yyn);
            yyastk[yysp] = yylval;
            yychar1 = yychar = -1;

            if (yyerrflag > 0)
              yyerrflag--;
            if (yyn < YYNLSTATES)
              continue;

            /* yyn >= YYNLSTATES means shift-and-reduce */
            yyn -= YYNLSTATES;
          } else
            yyn = -yyn;
        } else
          yyn = yydefault[yystate];
      }

      for (;;) {
        /* reduce/error */
        if (yyn == 0) {
          /* accept */
          return 0;
        }

	boolean yyparseerror = true;
	if (yyn != YYUNEXPECTED) {
          /* reduce */
	  yyparseerror = false;
          yyl = yylen[yyn];
          yyval = yyastk[yysp-yyl+1];
	  int yylrec = 0;
          /* Following line will be replaced by reduce actions */
          switch(yyn) {
          case 1:
{
					yyval = ((List<SExpression>)yyastk[yysp-(1-1)]);
				} break;
          case 2:
{
					List<SExpression> texpression_ary = new ArrayList<SExpression>();
					texpression_ary.add(((SExpression)yyastk[yysp-(1-1)]));
					yyval = texpression_ary;
				} break;
          case 3:
{
					((List<SExpression>)yyastk[yysp-(2-1)]).add(((SExpression)yyastk[yysp-(2-2)]));
					yyval = ((List<SExpression>)yyastk[yysp-(2-1)]);
				} break;
          case 4:
{ yyval = ((Node)yyastk[yysp-(1-1)]); } break;
          case 5:
{ yyval = ((Leaf)yyastk[yysp-(1-1)]); } break;
          case 6:
{ yyval = new DefaultNode(((List<SExpression>)yyastk[yysp-(3-2)])); } break;
          case 7:
{ yyval = new ArrayList<SExpression>(); } break;
          case 8:
{
					((List<SExpression>)yyastk[yysp-(2-1)]).add(((SExpression)yyastk[yysp-(2-2)]));
					yyval = ((List<SExpression>)yyastk[yysp-(2-1)]);
				} break;
          case 9:
{ yyval = new DefaultLeaf(((String)yyastk[yysp-(1-1)])); } break;
          }
	  if (!yyparseerror) {
            /* Goto - shift nonterminal */
            yysp -= yyl;
            yyn = yylhs[yyn];
            if ((yyp = yygbase[yyn] + yysstk[yysp]) >= 0 && yyp < YYGLAST
                && yygcheck[yyp] == yyn)
              yystate = yygoto[yyp];
            else
              yystate = yygdefault[yyn];

            if (++yysp >= yysstk.length)
              growStack();

            yysstk[yysp] = (short)yystate;
            yyastk[yysp] = yyval;
	  }
	}

	if (yyparseerror) {
          /* error */
          switch (yyerrflag) {
          case 0:
            yyerror("syntax error");
          case 1:
          case 2:
            yyerrflag = 3;
            /* Pop until error-expecting state uncovered */

            while (!((yyn = yybase[yystate] + YYINTERRTOK) >= 0
                     && yyn < YYLAST && yycheck[yyn] == YYINTERRTOK
                     || (yystate < YY2TBLSTATE
                         && (yyn = yybase[yystate + YYNLSTATES] + YYINTERRTOK) >= 0
                         && yyn < YYLAST && yycheck[yyn] == YYINTERRTOK))) {
              if (yysp <= 0)
                return 1;
              yystate = yysstk[--yysp];
            }
            yyn = yyaction[yyn];
            yysstk[++yysp] = (short)(yystate = yyn);
            break;

          case 3:
            if (yychar1 == 0)
              return 1;
	    yychar1 = yychar = -1;
            break;
          }
        }

        if (yystate < YYNLSTATES)
          break;
        /* >= YYNLSTATES means shift-and-reduce */
        yyn = yystate - YYNLSTATES;
      }
    }
  }

}




